package com.qinggeng.project.api.controller;

import com.qinggeng.common.exception.CustomException;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.security.annotation.LoginRequired;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.OrdersRecord;
import com.qinggeng.project.system.domain.SinglePointSale;
import com.qinggeng.project.system.domain.SumPointSale;
import com.qinggeng.project.system.domain.vo.SinglePointSaleVo;
import com.qinggeng.project.system.domain.vo.SumPointSaleVo;
import com.qinggeng.project.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @author 吴鹏飞
 * @date 2020/11/19
 */
@Slf4j
@Api(tags = "自提点销量打印")
@RestController
@RequestMapping("${apiPath}/printPointSale")
public class ApiPrintPointSaleController extends BaseController {
    @Autowired
    private SinglePointSaleService singlePointSaleService;
    @Autowired
    private SumPointSaleService sumPointSaleService;
    @Autowired
    private IDistributionPointService distributionPointService;

    @Value("${pointKey.secretKey}")
    private String secretKey;


    @Autowired
    private OrdersArerRecordService ordersArerRecordService;


    @Autowired
    private OrdersPointRecordService ordersPointRecordService;

    @Autowired
    private IOrdersRecordService ordersRecordService;

    private static final String POINT_ID_ = "point_id_";

    @GetMapping("/findAllPoint")
    @ApiOperation("查询查询所有自提点")
    public AjaxResult findAllPoint(String secretKey,String recordDate){
        try {
            if (!secretKey.equals(this.secretKey)){
                throw new CustomException("密钥不对");
            }
            List<String>   pointIds=ordersPointRecordService.listOrdersPointRecordPointId(recordDate);

            return AjaxResult.success(pointIds);
        } catch (CustomException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    @GetMapping("/oneClickReceipt")
    @ApiOperation("自提点一键收货")
    @LoginRequired(role = ApiUtils.DEFAULT_ROLE_COURIER)
    public AjaxResult oneClickReceipt(String pointId){
        Set<String> failTakes = distributionPointService.oneClickReceipt(pointId);
        return AjaxResult.success("取货成功",failTakes);
    }

    @GetMapping("/findAllPointArea")
    @ApiOperation("查询所有自提点区域")
    public AjaxResult findAllPointArea(String secretKey,String recordDate){
        try {
            if (!secretKey.equals(this.secretKey)) {
                throw new CustomException("密钥不对");
            }
            List<String>   sortAreas=ordersArerRecordService.listOrdersArerRecordName(recordDate);
            return AjaxResult.success(sortAreas);
        } catch (CustomException e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    @GetMapping("/findSinglePointSaleRecord")
    @ApiOperation("查询单自提点销量")
    public AjaxResult findSinglePointSaleRecord(Long pointId,String secretKey,String recordDate){
        try {
            if (!secretKey.equals(this.secretKey)){
                throw new CustomException("密钥不对");
            }

            List<SinglePointSaleVo> singlePointSaleVos = singlePointSaleService.listSinglePointSaleByYesterday(pointId,recordDate);
            return AjaxResult.success(singlePointSaleVos);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/checkSinglePointSaleRecordPrintStatus")
    @ApiOperation("校验单自提点销量打印状态")
    public AjaxResult checkSinglePointSaleRecordPrintStatus(Long id,String secretKey){
        try {
            if (!secretKey.equals(this.secretKey)){
                throw new CustomException("密钥不对");
            }
            SinglePointSale singlePointSale = singlePointSaleService.selectSinglePointById(id);
            return AjaxResult.success(singlePointSale);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    @GetMapping("/printPointSaleRecord")
    @ApiOperation("打印单自提点销量成功")
    public AjaxResult printPointSaleRecord(Long id,String secretKey){
            if (!secretKey.equals(this.secretKey)){
                throw new CustomException("密钥不对");
            }
            boolean bl=singlePointSaleService.printSinglePointSaleList(id);
            if (bl){
                return AjaxResult.success("修改成功");

            }else {
                return AjaxResult.success("修改失败");
            }
    }

    @GetMapping("/findSumlePointSaleRecord")
    @ApiOperation("查询区域自提点销量")
    public AjaxResult findSumlePointSaleRecord(String sortArea,String secretKey,String recordDate){
        try {
            if (!secretKey.equals(this.secretKey)){
                throw new CustomException("密钥不对");
            }
            List<SumPointSaleVo> sumPointSaleVos = sumPointSaleService.listSumPointSaleByYesterday(sortArea,recordDate);
            return AjaxResult.success(sumPointSaleVos);
        } catch (CustomException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    @GetMapping("/findSinglePointSaleCount")
    @ApiOperation("查询自提点订单")
    public AjaxResult findSinglePointSaleCount(Long pointId,String secretKey,String recordDate){
        try {
            if (!secretKey.equals(this.secretKey)){
                throw new CustomException("密钥不对");
            }
            final List<OrdersRecord> list = ordersRecordService.listOrdersRecordByPointIdAndRecordDate(pointId, recordDate);
            if (null == list || list.isEmpty()){
                return AjaxResult.success(0);
            }
            return AjaxResult.success(list.size());

        } catch (CustomException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/checkSumlePointSaleRecordPrintStatus")
    @ApiOperation("查询区域自提点销量")
    public AjaxResult checkSumlePointSaleRecordPrintStatus(Long id,String secretKey){
        try {
            if (!secretKey.equals(this.secretKey)){
                throw new CustomException("密钥不对");
            }
            startPage();
            SumPointSale sumPointSale = sumPointSaleService.selectSumlePointSaleRecordById(id);
            return AjaxResult.success(sumPointSale);
        } catch (CustomException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/printSumSaleRecord")
    @ApiOperation("打印区域自提点销量成功")
    public AjaxResult printSumSaleRecord(String id,String secretKey){
        try {
            if (!secretKey.equals(this.secretKey)){
                throw new CustomException("密钥不对");
            }
            sumPointSaleService.updatePrintStatusById(id);
            return AjaxResult.success("修改成功");
        } catch (CustomException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
